home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5058 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  114 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: fopen not working
  5. Date: Mon, 12 Feb 96 20:55:53 GMT
  6. Organization: none
  7. Message-ID: <824158553snz@genesis.demon.co.uk>
  8. References: <4fjnj1$4q@fig.leba.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4fjnj1$4q@fig.leba.net> fweaver@leba.net  writes:
  15.  
  16. >Good evening to all C programers
  17. >
  18. >   I have a problem I thought someone might be able to shed some light on 
  19. >for me. Below is some of the code I wrote for an install program.
  20. >
  21. >
  22. >#include<stdio.h>
  23. >#include<conio.h>
  24.  
  25. There doesn't appear to be anything here that need conio.h. Also it isn't
  26. a standard C header file so is best avoided when posting to comp.lang.c
  27.  
  28. >char buf[125],stwd[110],trwd[110],*pt,*pt1;
  29. >int i,vr,vd;
  30. >FILE *in, *out;
  31. >main()
  32. >{   [snip]
  33. > instlfile("trkr.cf_","trkr.cfg");
  34. > instlfile("maplvl3.da_","maplvl3.dat");
  35. > instlfile("atpctrkr.do_","atpctrkr.doc");
  36. > instlfile("readme.1s_","readme.1st");
  37. > instlfile("file_id.di_","file_id.diz");
  38. > [snip]
  39. >
  40.  
  41. > if((in=fopen(buf,"r"))!=NULL)
  42. >  { do{ fgets(buf,120,in);
  43. >   pt= (char *)strstr(buf,"XXXXX");
  44. >   if(pt!=NULL){ fclose(out); pt=pt+5;if((out=fopen(pt,
  45. >"w"))==NULL)printf("ERROR");
  46. > else{printf("\nInstalling %s",pt);continue; }}
  47. >   fputs(buf,out);
  48. > }while(feof(in)==0);
  49. > }
  50. >}
  51.  
  52. This is almost impossible to read. I've reformatted into something clearer.
  53. It isn't the idea of C to cram a program into as few lines as possible.
  54.  
  55. >>    if ((in=fopen(buf,"r")) != NULL) {
  56. >>        do {
  57. >>            fgets(buf,120,in);
  58.  
  59. What is 120? If you want to pass the size of buf then use sizeof buff, or
  60. perhaps better define a constant and use that in both places.
  61.  
  62. fgets can fail and return NULL which you need to test then and there. This
  63. sort of loop normally takes the form:
  64.  
  65.           while (fgets(buf, sizeof buf, in) != NULL) {
  66.             ...
  67.           }
  68.  
  69. >>            pt = (char *)strstr(buf,"XXXXX");
  70.  
  71. Why the cast? Did you include string.h? If not your compiler probably produced
  72. a warning. The cast may stop the warning being generated but the compiler
  73. will still generate code as if strstr() returned an int.
  74.  
  75. >>            if (pt != NULL) {
  76. >>                fclose(out);
  77.  
  78. You'd better make sure that out contains a valid file pointer value e.g.
  79. NULL is not legal here. Check especially the first pass through the loop.
  80.  
  81. >>                pt=pt+5;
  82.  
  83. The normal C idiom is pt += 5;
  84.  
  85. >>                if ((out=fopen(pt, "w")) == NULL)
  86.  
  87. That looks OK. Are you sure it is exactly what you have in your code e.g.
  88. not == instead of =?
  89.  
  90. >>                    printf("ERROR");
  91. >>                else {
  92. >>                    printf("\nInstalling %s",pt);
  93. >>                    continue;
  94. >>                }
  95. >>            }
  96. >>            fputs(buf, out);
  97. >>        } while (feof(in)==0);
  98.  
  99. It is very rarely that you need to use feof in C. Anyway this is far too
  100. late since by the time you've detected that the line hasn't been read in
  101. you've already tried to process it. The while (fgets() != NULL) loop above
  102. is the correct approach.
  103.  
  104. >>    }
  105. >>}
  106.  
  107. -- 
  108. -----------------------------------------
  109. Lawrence Kirby | fred@genesis.demon.co.uk
  110. Wilts, England | 70734.126@compuserve.com
  111. -----------------------------------------
  112.